﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using System.Xml.Serialization;
using SimDev.Config;
namespace SimDev.ViewModel
{
 
    public class MainViewModel:ViewModelBase
    {
        ObservableCollection<DTU> _DTUs = null;
 
       
        public DevConfig DevConfig
        {
            get;
            private set;
        }

        public MainViewModel(DevConfig config)
        {
            DevConfig = config;

            RandomDTUCount = 25;

       
       
            ServerIP= config.ServerIP;
            Port = config.Port;

        
                config.DTUs = new List<DTUConfig>();
                _DTUs = new ObservableCollection<DTU>();
           
        }

       
 

        int _RandomDTUCount = 25;
        public int RandomDTUCount
        {
            get
            {
                return _RandomDTUCount;
            }
            set
            {
                _RandomDTUCount = value;
                
                RaisePropertyChanged("RandomDTUCount");
            }
        }

        int _DataGroupCount = 1;
        public int DataGroupCount
        {
            get
            {
                return _DataGroupCount;
            }
            set
            {
                _DataGroupCount = value;

                RaisePropertyChanged("DataGroupCount");
            }
        }

        internal void StopSendData()
        {
            Array.ForEach<DTU>(_DTUs.ToArray(), d =>
            {
                d.Stop();
            });
        }

        string _ServerIP = "127.0.0.1";
 
        public string ServerIP
        {
            get { return _ServerIP; }
            set
            {
                _ServerIP = value;
                
                RaisePropertyChanged("ServerIP");
            }
        }



        int _Port = 9074;
 
        public int Port
        {
            get { return _Port; }
            set
            {
                _Port = value;
               
                RaisePropertyChanged("Port");
            }
        }
 

    
 
 
 

        public ObservableCollection<DTU> DTUs
        {
            get
            {
                return _DTUs;
            }
        }
 

        public void CreateSimDTU()
        {
           
    
            int createdCount = 0;
            while (createdCount < RandomDTUCount)
            {
              
               
                    _DTUs.Add(new DTU(DataGroupCount));
                    ++createdCount;
              
            }
        }

  

        public void SendOnceData() {
            if(_DTUs.Count==0)CreateSimDTU();

            Array.ForEach<DTU>(_DTUs.ToArray(), d =>
            {
                d.SendOnceData(ServerIP, Port);
            });
        }

        public void AutoSendData()
        {
            if (_DTUs.Count == 0) CreateSimDTU();
            Array.ForEach<DTU>(_DTUs.ToArray(), d =>
            {
                d.AutoSendData(ServerIP, Port);
            });
        }
        internal void DisConnect()
        {
            Array.ForEach<DTU>(_DTUs.ToArray(), d =>
            {
                d.DisConnect();
            });
        }



      
    }

    public class RTUFactoryInfo
    {
     
        public Type RTUType { get; set; }

        public string RTUModel { get; set; }

        public List<Register> Register{get;set;} 

        public bool Selected { get; set; }
    }
}
